package com.fileshare.web.controller.admin.view;

import com.fileshare.web.controller.admin.BaseController;
import com.fileshare.web.dto.info.WebInfo;
import com.fileshare.web.entity.Authority;
import com.fileshare.web.entity.Role;
import com.fileshare.web.entity.User;
import com.fileshare.web.service.AuthService;
import com.fileshare.web.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端权限管理页
 */
@Controller
@RequestMapping("/admin/auth")
@PreAuthorize("@auth.matchAny('administrator')")
public class AuthAdminController extends BaseController {
    @Resource
    private AuthService authService;
    @Resource
    private UserService userService;

    @GetMapping("/role")
    public String role(HttpServletRequest request) {
        WebInfo webInfo = new WebInfo();
        webInfo.setTitle("角色管理");
        request.setAttribute("webInfo", webInfo);
        List<Role> roles = authService.getAllRoles();
        request.setAttribute("roles", roles);
        return view("auth/role");
    }

    @GetMapping("/authority")
    public String authority(HttpServletRequest request) {
        WebInfo webInfo = new WebInfo();
        webInfo.setTitle("权限管理");
        List<Authority> authorities = authService.getAllAuthorities();
        request.setAttribute("webInfo", webInfo);
        request.setAttribute("authorities", authorities);
        return view("auth/authority");
    }

    @GetMapping("/user")
    public String user(HttpServletRequest request) {
        WebInfo webInfo = new WebInfo();
        webInfo.setTitle("用户权限");
        List<User> users = userService.getUsers();
        request.setAttribute("webInfo", webInfo);
        request.setAttribute("users", users);
        return view("auth/user");
    }
}
